/*
 * IC Project - a prototype to test a hypothesis of collective intelligence
 * Copyright (C) 2012 Paolo Conte - paolo@concavi.net
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ix.data.entities;

public class P2PMessage {

    public static String ACTION_HELO = "helo";
    public static String ACTION_JOIN = "join";
    public static String ACTION_LIST = "list";
    public static String ACTION_NAME = "name";
    public static String ACTION_QUIT = "quit";
    public static String RESPONSE_OK = "OK";
    public static String RESPONSE_KO = "KO";
    public static String RESPONSE_TYPE_REPLY = "REPLY";
    public static String RESPONSE_TYPE_ERROR = "ERROR";
    public String action = null;
    public String status = null;
    public String type = null;
    public String content = null;

    public P2PMessage(String action, String type, String content) {
        this(action, null, type, content);
    }

    public P2PMessage(String action, String status, String type, String content) {
        if (status == null && type != null && type.equals(RESPONSE_TYPE_REPLY)) {
            status = RESPONSE_OK;
        }
        this.action = action;
        this.status = status;
        this.type = type;
        this.content = content;
    }
    
    public String toString() {
        return action + ": " + status + ", " + type + ", " + content;
    }
    
    public boolean isOK() {
        return (status!=null && status.equals(RESPONSE_OK) || isReply());
    }
    
    public boolean isReply() {
        return (type!=null && type.equals(RESPONSE_TYPE_REPLY));
    }
}
